package com.zn.web.rpc;

import com.zn.util.HttpClientUtil;
import com.zn.vo.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author YinJiaWei
 * @version 2018/8/25 20:12
 */
@Component
public class MdRemoteCall {
    private static final Logger LOGGER = LoggerFactory.getLogger(MdRemoteCall.class);
    @Value("${jiejiari.url}")
    private String url;
    @Value("${jiejiari.interface.checkholiday}")
    private String checkHoliday;
    @Value("${sinajs.ip}")
    private String sinajsIp;
    @Value("${sinajsetf.ip}")
    private String sinajsetfIp;
    @Value("${sinajs.option.url}")
    private String sinajsOptionUrl;
    @Value("${sinajs.option.min.line}")
    private String sinajsOptionMinLine;
    @Value("${sinajs.option.fiveDay.line}")
    private String sinajsOptionFiveDayLine;
    @Value("${sinajs.option.day.line}")
    private String sinajsOptionDayLine;
    @Value("${eastmoney.month.url}")
    private String eastmoneyMonthUrl;
    @Value("${totality.task.url}")
    private String totalityTaskUrl;

    public Response checkHoliday(String date){
        Response response = HttpClientUtil.post(url,
                HttpClientUtil.Builder.builder(checkHoliday)
                        .with("date", date)
        );
        return response;
    }

    public String getSinaOptionMinLine(String targetId){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("?symbol=CON_OP_" + targetId), sinajsIp + sinajsOptionUrl + sinajsOptionMinLine);
        return sinaTick;
    }

    public String getSinaOptionDayLine(String targetId){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("?symbol=CON_OP_" + targetId), sinajsIp + sinajsOptionUrl + sinajsOptionDayLine);
        return sinaTick;
    }

    public String getSinaOptionFiveDayLine(String targetId){
        String sinaTick=HttpClientUtil.get(HttpClientUtil.Builder.builder("?symbol=CON_OP_" + targetId), sinajsIp + sinajsOptionUrl + sinajsOptionFiveDayLine);
        return sinaTick;
    }

    public String getEastmoneyMonthLine(String targetId){
        String sinaTick=eastmoneyMonthUrl+"&id="+targetId+"_SO";
        return sinaTick;
    }

    public String getEtfMdInfo(String targetCode){
        //https://stock.finance.sina.com.cn/list=s_sh510050
        String etfMdStr=HttpClientUtil.get(HttpClientUtil.Builder.builder("/list=s_sh"+targetCode), sinajsetfIp);
        return etfMdStr;
    }

    public String [] subStringSinaData(String context){
        String [] mdInfoArray=context.split("=");
        if(mdInfoArray.length<2){
            return null;
        }
        String mdInfoElement=mdInfoArray[mdInfoArray.length-1];
        String mdInfoStr=mdInfoElement.substring(1,mdInfoElement.length()-2);
        String [] mdInfos=mdInfoStr.split(",");
        return mdInfos;
    }

    public void sendSocketStatus(String businessUnique,Integer listenerType,Integer socketStatus){
        try{
            HttpClientUtil.post(totalityTaskUrl,
                    HttpClientUtil.Builder.builder("/management/readSocketStatus")
                            .with("businessUnique", businessUnique)
                            .with("listenerType", listenerType)
                            .with("socketStatus", socketStatus)
            );
        }catch (Exception e){
            LOGGER.error("sendSocketStatus......"+e);
        }
    }
}
